/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F240
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include "stdlib.h"
#include <SN32F280.h>
#include "LED_Music.h"
#include "LED_Code.h"
#include "LED_Driver.h"
#include "..\Project\UserSet.h"
#include "..\Driver\PWM.h"
#include "..\KB\KB_API.h"
#include "..\KB\KB_BT.h"
#include "..\KB\KB_RF_API.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/

/*_____ D E F I N I T I O N S ______________________________________________*/
uint8_t rMusicIndex_R = 0,rMusicIndex_G = 64, rMusicIndex_B = 128;
uint8_t rMusicLED_buf[45],gMusicLED_buf[45],bMusicLED_buf[45];
uint8_t RMusic1_Base_Buf[LED_NUM],GMusic1_Base_Buf[LED_NUM],BMusic1_Base_Buf[LED_NUM];
uint8_t rMusicLED_Cnt;
/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/
/*****************************************************************************
* Function		: MusicLED_Init
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void MusicLED_Init(void)
{
	uint8_t i,j;
	uint16_t k;
	
	for(i=0;i<LED_MODE6_TAB_LEN;i++)
	{
		k = 64+LED_M_ROTATE_INIT_TAB[i];
		if(k>=LED_SPECTRUM_TAB_LEN)
			k -= LED_SPECTRUM_TAB_LEN;
		rMusicLED_buf[i] = k;
		
		k = 0+LED_M_ROTATE_INIT_TAB[i];
		if(k>=LED_SPECTRUM_TAB_LEN)
			k -= LED_SPECTRUM_TAB_LEN;
		gMusicLED_buf[i] = k;
		
		k = 128+LED_M_ROTATE_INIT_TAB[i];
		if(k>=LED_SPECTRUM_TAB_LEN)
			k -= LED_SPECTRUM_TAB_LEN;
		bMusicLED_buf[i] = k;
		
		for(j=0;j<3;j++)
		{
			RMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[rMusicLED_buf[i]];
			GMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[gMusicLED_buf[i]];
			BMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[bMusicLED_buf[i]];
		}
	}
}

/*****************************************************************************
* Function		: MusicLED_Display
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void MusicLED_Display(void)
{
	uint8_t i,j;
	rMusicLED_Cnt++;
	if(rMusicLED_Cnt<3)
		return;
	rMusicLED_Cnt = 0;
	
	if((rSys_mode == SYS_RF_MODE)&&(bAPP_LED_Customize))
	{
		rMusicIndex_R++;
		if(rMusicIndex_R>=LED_SPECTRUM_TAB_LEN)
			rMusicIndex_R = 0;
		
		rMusicIndex_G++;
		if(rMusicIndex_G>=LED_SPECTRUM_TAB_LEN)
			rMusicIndex_G = 0;
		
		rMusicIndex_B++;
		if(rMusicIndex_B>=LED_SPECTRUM_TAB_LEN)
			rMusicIndex_B = 0;
		

		for(i=0;i<LED_MODE6_TAB_LEN;i++)
		{
			if(rMusicLED_buf[i] == 0)
			{
				rMusicLED_buf[i] = LED_SPECTRUM_TAB_LEN -1;
			}
			else
				rMusicLED_buf[i] --;
			
			if(gMusicLED_buf[i] == 0)
			{
				gMusicLED_buf[i] = LED_SPECTRUM_TAB_LEN -1;
			}
			else
				gMusicLED_buf[i] --;
			
			if(bMusicLED_buf[i] == 0)
			{
				bMusicLED_buf[i] = LED_SPECTRUM_TAB_LEN -1;
			}
			else
				bMusicLED_buf[i] --;

			for(j=0;j<3;j++)
			{
				RMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[rMusicLED_buf[i]];
				GMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[gMusicLED_buf[i]];
				BMusic1_Base_Buf[LED_MODE6_TAB[i][j]] = LED_SPECTRUM_TAB[bMusicLED_buf[i]];
			}
		}	
		
		rMusic_LED.Mode = rRF_API_RX_Buf[3];
		rMusic_LED.BMode = rRF_API_RX_Buf[4];
		rMusic_LED.Light = rRF_API_RX_Buf[5];
		for(i=0;i<LED_COL_NUM;i++)
			rMusic_LED.Value[i] = rRF_API_RX_Buf[6+i];
		
		for(i=0;i<LED_COL_NUM;i++)
		{
			for(j=0;j<6;j++)
			{
				if(j<rMusic_LED.Value[i])
				{
					MusicLED_Switch(rMusic_LED.Mode,i,j);		
				}
				else
				{
					MusicLED_Switch(rMusic_LED.BMode,i,j);
				}
			}
		}
	}
}

/*****************************************************************************
* Function		: MusicLED_Switch
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void MusicLED_Switch(uint8_t mode,uint8_t col,uint8_t row)
{
	uint8_t k;
	k = LED_POSITION_TAB[col][5-row];
	switch(mode)
	{
		case 0:
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = 0;			
			break;
		case 1:
			if(row <=1)
			{
				iRPWM_Buf[k] = PWM_MAX;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = 0;				
			}
			else if(row <=3)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = 0;				
			}
			else
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX;
				iBPWM_Buf[k] = 0;				
			}
			break;
		case 2:
			if(row == 0)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = 0;			
			}
			else if(row == 1)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = 0;							
			}
			else if(row == 2)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX;
				iBPWM_Buf[k] = 0;					
			}
			else if(row == 3)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = PWM_MAX/2;					
			}
			else if(row == 4)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = PWM_MAX;						
			}
			else if(row == 5)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = PWM_MAX/2;					
			}
			break;
		case 3:
			if(row == 0)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = PWM_MAX/2;				
			}
			else if(row == 1)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = PWM_MAX;						
			}
			else if(row == 2)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = PWM_MAX/2;						
			}
			else if(row == 3)
			{
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX;
				iBPWM_Buf[k] = 0;						
			}
			else if(row == 4)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = 0;						
			}
			else if(row == 5)
			{
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = 0;						
			}
			break;
		case 4:
				iRPWM_Buf[k] = RMusic1_Base_Buf[k];
				iGPWM_Buf[k] = GMusic1_Base_Buf[k];
				iBPWM_Buf[k] = BMusic1_Base_Buf[k];					
			break;
		case 5:
				iRPWM_Buf[k] = LED_SPECTRUM_TAB[rMusicIndex_R];
				iGPWM_Buf[k] = LED_SPECTRUM_TAB[rMusicIndex_G];
				iBPWM_Buf[k] = LED_SPECTRUM_TAB[rMusicIndex_B];				
			break;
		case 6:
				iRPWM_Buf[k] = PWM_MAX;
				iGPWM_Buf[k] = PWM_MAX;
				iBPWM_Buf[k] = PWM_MAX;	
			break;
		case 7:
				iRPWM_Buf[k] = PWM_MAX;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = 0;	
			break;
		case 8:
				iRPWM_Buf[k] = PWM_MAX;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = 0;	
			break;
		case 9:
				iRPWM_Buf[k] = PWM_MAX/2;
				iGPWM_Buf[k] = PWM_MAX/2;
				iBPWM_Buf[k] = 0;	
			break;
		case 10:
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = 0;
				iBPWM_Buf[k] = PWM_MAX;	
			break;
		case 11:
				iRPWM_Buf[k] = 0;
				iGPWM_Buf[k] = PWM_MAX;
				iBPWM_Buf[k] = 0;	
			break;
		default:
			break;
	}
}
